package view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

import controller.ACSystem;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.LineBorder;

import model.Address;
import model.AuctionCentralEmployee;
import model.Bidder;
import model.NonProfit;
import model.User;
import model.WestPanelEnum;

import java.awt.Color;
import javax.swing.JPasswordField;

/**
 * The NewUserPane
 * 
 * @author Bob Landowski
 */

@SuppressWarnings("serial")
public class NewUserPane extends JPanel {

	/** The first name textField */
	private JTextField firstNameField;

	/** The last name textField */
	private JTextField lastNameField;

	/** The address textField */
	private JTextField address1Field;

	/** The address2 textField */
	private JTextField address2Field;

	/** The phone textField */
	private JTextField phoneField;

	/** The email textField */
	private JTextField emailField;

	/** The city textField */
	private JTextField cityField;

	/** The state textField */
	private JTextField stateField;

	/** The zip code textField */
	private JTextField zipCodeField;

	/** The first name textField */
	private ACSystem my_ACcontroller;
	
	/** The access level */
	private int my_access_level;

	/** The address */
	private Address my_address;

	User my_new_user;
     
	/** The userName field */
	private JTextField userNameField;
	
	/** The organization field */
	private JTextField organizationField;
	
	/** The password field */
	private JPasswordField passwordField;

	/**
	 * Create the panel.
	 */
	public NewUserPane(final ACSystem the_ACcontroller) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		my_ACcontroller = the_ACcontroller;
		setLayout(null);
		setPreferredSize(new Dimension(500, 500));
		JLabel firstnameLabel = new JLabel("First name:");
		firstnameLabel.setBounds(15, 76, 85, 20);
		add(firstnameLabel);

		firstNameField = new JTextField();
		firstNameField.setBounds(130, 76, 153, 20);
		add(firstNameField);
		firstNameField.setColumns(10);

		JLabel lastNameLabel = new JLabel("Last name:");
		lastNameLabel.setBounds(15, 106, 100, 20);
		add(lastNameLabel);

		lastNameField = new JTextField();
		lastNameField.setColumns(10);
		lastNameField.setBounds(130, 106, 153, 20);
		add(lastNameField);

		JLabel address1_label = new JLabel("Address:");
		address1_label.setBounds(15, 196, 69, 20);
		add(address1_label);

		address1Field = new JTextField();
		address1Field.setColumns(10);
		address1Field.setBounds(130, 196, 275, 20);
		add(address1Field);

		JLabel address_label2 = new JLabel("Address:");
		address_label2.setBounds(15, 226, 69, 20);
		add(address_label2);

		address2Field = new JTextField();
		address2Field.setColumns(10);
		address2Field.setBounds(130, 226, 275, 20);
		add(address2Field);

		JLabel phone_label = new JLabel("Phone:");
		phone_label.setBounds(15, 316, 69, 20);
		add(phone_label);

		phoneField = new JTextField();
		phoneField.setColumns(10);
		phoneField.setBounds(130, 316, 87, 20);
		add(phoneField);

		JLabel email_label = new JLabel("Email:");
		email_label.setBounds(249, 286, 69, 20);
		add(email_label);

		emailField = new JTextField();
		emailField.setColumns(10);
		emailField.setBounds(308, 286, 100, 20);
		add(emailField);

		JCheckBox userLevel2 = new JCheckBox("AC Employee");
		userLevel2.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				my_access_level = 2;
			}
		});
		userLevel2.setBounds(284, 346, 139, 29);
		add(userLevel2);

		JCheckBox userLevel3 = new JCheckBox("Non-Profit");
		userLevel3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_access_level = 3;
			}
		});
		userLevel3.setBounds(139, 346, 115, 29);
		add(userLevel3);

		JCheckBox userLevel4 = new JCheckBox("Bidder");
		userLevel4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_access_level = 4;
			}
		});
		userLevel4.setBounds(15, 346, 100, 29);
		add(userLevel4);

		JLabel cityLabel = new JLabel("City:");
		cityLabel.setBounds(15, 256, 47, 20);
		add(cityLabel);

		cityField = new JTextField();
		cityField.setBounds(130, 256, 153, 20);
		add(cityField);
		cityField.setColumns(10);

		JLabel stateLabel = new JLabel("State:");
		stateLabel.setBounds(308, 256, 47, 20);
		add(stateLabel);

		stateField = new JTextField();
		stateField.setBounds(358, 256, 47, 20);
		add(stateField);
		stateField.setColumns(10);

		JLabel zipCodeLabel = new JLabel("Zip Code:");
		zipCodeLabel.setBounds(15, 286, 69, 20);
		add(zipCodeLabel);

		zipCodeField = new JTextField();
		zipCodeField.setBounds(130, 286, 85, 20);
		add(zipCodeField);
		zipCodeField.setColumns(10);

		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveUser();
				my_ACcontroller.setWestPanel(WestPanelEnum.LOGIN);
				my_ACcontroller.getMainFrame().westPanelState(new Object());
			}
		});
		submitButton.setBounds(53, 387, 115, 29);
		add(submitButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		cancelButton.setBounds(228, 387, 115, 29);
		add(cancelButton);

		JLabel userNameLabel = new JLabel("User Name:");
		userNameLabel.setBounds(15, 136, 87, 20);
		add(userNameLabel);

		userNameField = new JTextField();
		userNameField.setBounds(130, 136, 153, 20);
		add(userNameField);
		userNameField.setColumns(10);

		JLabel titleLabel = new JLabel("Auction Central New User Request");
		titleLabel.setBounds(111, 16, 256, 20);
		add(titleLabel);

		JLabel passwordLabel = new JLabel("Password:");
		passwordLabel.setBounds(15, 166, 85, 20);
		add(passwordLabel);

		JLabel organizationLabel = new JLabel("Organization:");
		organizationLabel.setBounds(15, 46, 100, 20);
		add(organizationLabel);

		organizationField = new JTextField();
		organizationField.setBounds(130, 46, 153, 20);
		add(organizationField);
		organizationField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(130, 166, 153, 20);
		add(passwordField);
	}

	/** The saveUser */
	@SuppressWarnings("deprecation")
	public void saveUser() {
		/*
		 * 1- AuctionCentral Director 2- Auction Central Employee 3- Non-profit
		 * 4- Bidder
		 */

		my_address = new Address(address1Field.getText(),
				address2Field.getText(), null, cityField.getText(),
				cityField.getText(), zipCodeField.getText());

		switch (my_access_level) {
		case 2:
			AuctionCentralEmployee my_new_ACemployee = new AuctionCentralEmployee(
					firstNameField.getText(), lastNameField.getText(),
					userNameField.getText(), passwordField.getText(),
					phoneField.getText(), my_address, emailField.getText(),
					null);
			my_ACcontroller.addUser(my_new_ACemployee);
			break;
		case 3:

			NonProfit my_new_nonProfit = new NonProfit(
					firstNameField.getText(), lastNameField.getText(),
					userNameField.getText(), passwordField.getText(),
					phoneField.getText(), my_address, emailField.getText(),
					organizationField.getText());
			my_ACcontroller.addUser(my_new_nonProfit);
			break;
		case 4:

			Bidder my_new_bidder = new Bidder(firstNameField.getText(),
					lastNameField.getText(), userNameField.getText(),
					passwordField.getText(), phoneField.getText(), my_address,
					emailField.getText());
			my_ACcontroller.addUser(my_new_bidder);
			break;
		}

	}
}
